#include <QtGui>

#include "node.h"
#include <QStyleOptionGraphicsItem>
#include <QStyle>
#include <QLineEdit>
#include <QInputDialog>
#include <QFontMetricsF>
#include <QGraphicsSceneMouseEvent>
#include "Siim_Define.h"

Node::Node(int n,int w,int h)
{
    myTextColor = Qt::darkGreen;
    myOutlineColor = Qt::darkBlue;
    myBackgroundColor = Qt::white;
	m_width = w;
	m_height = h;
	m_shape= n;
   // setFlags(ItemIsMovable | ItemIsSelectable);
}

Node::~Node()
{
    
}

void Node::setText(const QString &text)
{
    prepareGeometryChange();
    myText = text;
    update();
}

QString Node::text() const
{
    return myText;
}

void Node::setTextColor(const QColor &color)
{
    myTextColor = color;
    update();
}

QColor Node::textColor() const
{
    return myTextColor;
}

void Node::setOutlineColor(const QColor &color)
{
    myOutlineColor = color;
    update();
}

QColor Node::outlineColor() const
{
    return myOutlineColor;
}

void Node::setBackgroundColor(const QColor &color)
{
    myBackgroundColor = color;
    update();
}

QColor Node::backgroundColor() const
{
    return myBackgroundColor;
}


QRectF Node::boundingRect() const
{
    const int Margin = 1;
    return outlineRect().adjusted(-Margin, -Margin, +Margin, +Margin);
}

QPainterPath Node::shape() const
{
    QRectF rect = outlineRect();

    QPainterPath path;
    path.addRoundRect(rect, roundness(rect.width()),
                      roundness(rect.height()));
    return path;
}

void Node::paint(QPainter *painter,
                 const QStyleOptionGraphicsItem *option,
                 QWidget * /* widget */)
{
    QPen pen(myOutlineColor);
    if (option->state & QStyle::State_Selected) {
        pen.setStyle(Qt::DotLine);
        pen.setWidth(2);
    }
    painter->setPen(pen);
    painter->setBrush(myBackgroundColor);

    QRectF rect = outlineRect();
   //painter->drawRoundRect(rect, roundness(rect.width()),
     //                      roundness(rect.height()));
	if(m_shape == s_Rect)
	{
		painter->drawRoundRect(rect.x(),rect.y(), m_width,
			m_height,0,0);
	}
	else
	{
		painter->drawEllipse(rect.x(),rect.y(), m_width,
			m_height);
	}
	 
    painter->setPen(myTextColor);
    painter->drawText(rect, Qt::AlignCenter, myText);
}

void Node::mouseDoubleClickEvent(QGraphicsSceneMouseEvent *event)
{
    QString text = QInputDialog::getText(event->widget(),
                           tr("Edit Text"), tr("Enter new text:"),
                           QLineEdit::Normal, myText);
    if (!text.isEmpty())
        setText(text);
}

QVariant Node::itemChange(GraphicsItemChange change,
                          const QVariant &value)
{
    if (change == ItemPositionHasChanged) {
       
    }
    return QGraphicsItem::itemChange(change, value);
}

QRectF Node::outlineRect() const
{
    const int Padding = 8;
   // QFontMetricsF metrics = qApp->font();
	QFontMetricsF metrics = (QFontMetricsF)qApp->font();
    QRectF rect = metrics.boundingRect(myText);
  //  rect.adjust(-Padding, -Padding, +Padding, +Padding);
  //  rect.translate(-rect.center());
  	rect.setWidth(m_width);
  	rect.setHeight(m_height);
    return rect;
}

int Node::roundness(double size) const
{
	if(size ==0)
	{
		return 25;
	}
    const int Diameter = 12;
    return 100 * Diameter / int(size);
}
